const express = require('express');
const path = require('path');
const cors = require('cors');
const bodyParser = require('body-parser');
const expressLayouts = require('express-ejs-layouts');
const multer = require('multer');
const fs = require('fs');
const nodemailer = require('nodemailer');
const svgCaptcha = require('svg-captcha');
const { readExcelFile, searchExcelData, getCategories, getPublisherCounts } = require('./utils/excelReader');
const { readBooksFile, searchBooks, getBookCategories, getPublisherCounts: getBooksPublisherCounts } = require('./utils/booksReader');
const { readSearchFile, searchSearchData, getSearchCategories } = require('./utils/searchReader');
const { clearCache, clearAllCache, getCacheStatus } = require('./utils/cacheManager');

const app = express();
const PORT = process.env.PORT || 3001;
const ITEMS_PER_PAGE = 10; // 每页显示10条数据

// 配置邮件发送器
const transporter = nodemailer.createTransport({
    service: 'qq', // 使用QQ邮箱服务
    host: 'smtp.qq.com', // QQ邮箱SMTP服务器
    port: 465, // SMTP端口
    secure: true, // 使用SSL
    auth: {
        user: '948479755@qq.com', // 发件人QQ邮箱，需要替换为真实的QQ邮箱
        pass: 'ooyyuttqyvqjbfcf' // 邮箱授权码，不是QQ密码
    }
});

// Configure multer for Excel file upload
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, path.join(__dirname, 'data'));
    },
    filename: function (req, file, cb) {
        // Choose filename based on data type
        const dataType = req.body.dataType || 'general';
        let filename;
        if (dataType === 'books') {
            filename = 'books.xlsx';
        } else if (dataType === 'search') {
            filename = 'search.xlsx';
        } else {
            filename = 'journals.xlsx';
        }
        cb(null, filename);
    }
});

// 根据文件类型清除相应的缓存
function clearCacheByFileType(fileType) {
    if (fileType === 'books') {
        clearCache('books');
        console.log('[Upload] Cleared books cache');
    } else if (fileType === 'search') {
        clearCache('search');
        console.log('[Upload] Cleared search cache');
    } else if (fileType === 'journals' || fileType === 'general') {
        clearCache('journals');
        console.log('[Upload] Cleared journals cache');
    } else {
        // 未知类型，清除所有缓存
        clearAllCache();
        console.log('[Upload] Cleared all caches');
    }
}

const upload = multer({
    storage: storage,
    fileFilter: function (req, file, cb) {
        // Accept only xlsx and xls files
        if (!file.originalname.match(/\.(xlsx|xls)$/)) {
            return cb(new Error('Please upload an Excel file!'), false);
        }
        cb(null, true);
    }
});

// Configure multer for image upload
const imageStorage = multer.diskStorage({
    destination: function (req, file, cb) {
        const uploadDir = path.join(__dirname, 'public/images');
        // 确保目录存在
        if (!fs.existsSync(uploadDir)) {
            fs.mkdirSync(uploadDir, { recursive: true });
        }
        cb(null, uploadDir);
    },
    filename: function (req, file, cb) {
        // 生成唯一文件名 - 时间戳 + 原始文件名
        const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
        const extension = path.extname(file.originalname);
        cb(null, 'cover-' + uniqueSuffix + extension);
    }
});

const imageUpload = multer({
    storage: imageStorage,
    fileFilter: function (req, file, cb) {
        // 只接受图片文件
        if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
            return cb(new Error('Please select a file to upload'), false);
        }
        cb(null, true);
    },
    limits: {
        fileSize: 1 * 1024 * 1024 // 限制1MB
    }
});

// Middleware
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));

// Use session to store captcha
const session = require('express-session');
app.use(session({
    secret: 'excel-web-app-secret',
    resave: false,
    saveUninitialized: false,
    cookie: { maxAge: 3600000 } // 1 hour expiration
}));

// View engine setup
app.use(expressLayouts);
app.set('layout', 'layout');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.set('layout extractScripts', true);
app.set('layout extractStyles', true);
app.set('layout extractMetas', true);

// Captcha route
app.get('/captcha', (req, res) => {
    const captcha = svgCaptcha.create({
        size: 4, // Captcha length
        ignoreChars: '0o1il', // Exclude confusing characters
        noise: 2, // Noise line count
        color: true, // Colored captcha
        background: '#f0f0f0' // Background color
    });

    req.session.captcha = captcha.text.toLowerCase(); // Save to session

    res.type('svg');
    res.status(200).send(captcha.data);
});

// Handle contact form submission
app.post('/contact', async (req, res) => {
    try {
        const { name, phone, email, message, captcha } = req.body;

        // Verify captcha
        if (!req.session.captcha || captcha.toLowerCase() !== req.session.captcha) {
            return res.render('contact-result', {
                title: 'Contact Us',
                currentPage: '',
                success: false,
                message: 'Invalid captcha, please try again.',
                searchQuery: ''
            });
        }

        // Clear captcha from session to prevent reuse
        req.session.captcha = null;

        // Build email content
        const mailOptions = {
            from: `"Contact Form" <948479755@qq.com>`, // Sender name and email
            to: `zhangdawangg@gmail.com`, // Recipient email
            subject: 'New Message from Website',
            html: `
                <h1>New Message from Website</h1>
                <p><strong>Name:</strong> ${name}</p>
                <p><strong>Phone:</strong> ${phone}</p>
                <p><strong>Email:</strong> ${email}</p>
                <p><strong>Message:</strong> ${message}</p>
            `
        };

        // Send email
        await transporter.sendMail(mailOptions);

        res.render('contact-result', {
            title: 'Contact Us',
            currentPage: '',
            success: true,
            message: 'Your message has been sent successfully. We will get back to you soon.',
            searchQuery: ''
        });
    } catch (error) {
        console.error('Error sending email:', error);
        res.render('contact-result', {
            title: 'Contact Us',
            currentPage: '',
            success: false,
            message: 'Error sending message, please try again later.',
            searchQuery: ''
        });
    }
});

// Routes
app.get('/', async (req, res) => {
    try {
        const page = parseInt(req.query.page) || 1;
        const category = req.query.category;

        // Load data types for display
        let data = await readExcelFile();
        let books = await readBooksFile();

        const categories = await getCategories();

        // 根据分类过滤数据
        if (category) {
            data = data.filter(item => {
                const itemCategory = item.category?.toLowerCase() || '';
                return itemCategory === category.toLowerCase();
            });
        }

        // 计算分页
        const totalItems = data.length;
        const totalPages = Math.ceil(totalItems / ITEMS_PER_PAGE);
        const startIndex = (page - 1) * ITEMS_PER_PAGE;
        const paginatedData = data.slice(startIndex, startIndex + ITEMS_PER_PAGE);

        res.render('index', {
            title: 'ISBN QUERY',
            currentPage: 'home',
            data: data, // All journals data for recommended section
            books: books, // All books data for recommended section
            totalItems: totalItems,
            itemsPerPage: ITEMS_PER_PAGE,
            category: category,
            categories: categories,
            pagination: {
                current: page,
                total: totalPages,
                hasNext: page < totalPages,
                hasPrev: page > 1
            },
            error: null,
            searchQuery: ''
        });
    } catch (error) {
        console.error('Error rendering index:', error);
        res.render('index', {
            title: 'ISBN QUERY',
            currentPage: 'home',
            data: [],
            books: [],
            totalItems: 0,
            itemsPerPage: ITEMS_PER_PAGE,
            category: req.query.category,
            categories: [],
            pagination: {
                current: 1,
                total: 1,
                hasNext: false,
                hasPrev: false
            },
            error: error.message,
            searchQuery: ''
        });
    }
});

app.get('/search', async (req, res) => {
    try {
        const { query, searchType, category } = req.query;
        const page = parseInt(req.query.page) || 1;
        let results = [];
        let error = null;

        if (query) {
            // 获取所有数据源的数据
            const journalsData = await readExcelFile();
            const booksData = await readBooksFile();
            const searchData = await readSearchFile();
            const searchTerm = query.toLowerCase();

            // 根据搜索类型进行筛选
            if (searchType && searchType !== 'all') {
                // 根据searchType筛选对应字段
                let journalResults = [];
                let bookResults = [];
                let searchResults = [];

                switch (searchType) {
                    case 'title':
                        // 期刊可能使用Journals字段或Title字段
                        journalResults = journalsData.filter(item =>
                            (item.Title && item.Title.toLowerCase().includes(searchTerm)) ||
                            (item.Journals && item.Journals.toLowerCase().includes(searchTerm))
                        );
                        // 图书使用Title字段
                        bookResults = booksData.filter(item =>
                            item.Title && item.Title.toLowerCase().includes(searchTerm)
                        );
                        // search数据过滤Title字段
                        searchResults = searchData.filter(item =>
                            item.Title && item.Title.toLowerCase().includes(searchTerm)
                        );
                        break;
                    case 'author':
                        journalResults = journalsData.filter(item =>
                            item.Authors && item.Authors.toLowerCase().includes(searchTerm)
                        );
                        bookResults = booksData.filter(item =>
                            item.Authors && item.Authors.toLowerCase().includes(searchTerm)
                        );
                        searchResults = searchData.filter(item =>
                            item.Authors && item.Authors.toLowerCase().includes(searchTerm)
                        );
                        break;
                    case 'isbn':
                        journalResults = journalsData.filter(item =>
                            item.ISBN && item.ISBN.toLowerCase().includes(searchTerm)
                        );
                        bookResults = booksData.filter(item =>
                            item.ISBN && item.ISBN.toLowerCase().includes(searchTerm)
                        );
                        searchResults = searchData.filter(item =>
                            item.ISBN && item.ISBN.toLowerCase().includes(searchTerm)
                        );
                        break;
                    case 'issn':
                        journalResults = journalsData.filter(item =>
                            item.ISSN && item.ISSN.toLowerCase().includes(searchTerm)
                        );
                        // 图书通常没有ISSN
                        searchResults = searchData.filter(item =>
                            item.ISSN && item.ISSN.toLowerCase().includes(searchTerm)
                        );
                        break;
                    case 'publisher':
                        journalResults = journalsData.filter(item =>
                            item.Publisher && item.Publisher.toLowerCase().includes(searchTerm)
                        );
                        bookResults = booksData.filter(item =>
                            item.Publisher && item.Publisher.toLowerCase().includes(searchTerm)
                        );
                        searchResults = searchData.filter(item =>
                            item.Publisher && item.Publisher.toLowerCase().includes(searchTerm)
                        );
                        break;
                    default:
                        journalResults = await searchExcelData(query);
                        bookResults = await searchBooks(query);
                        searchResults = await searchSearchData(query);
                }

                // 合并结果
                results = [
                    ...journalResults.map(item => ({ ...item, sourceType: 'journal' })),
                    ...bookResults.map(item => ({ ...item, sourceType: 'book' })),
                    ...searchResults.map(item => ({ ...item, sourceType: 'search' }))
                ];
            } else {
                // 默认全局搜索
                results = [
                    ...journalsData.map(item => ({ ...item, sourceType: 'journal' })),
                    ...booksData.map(item => ({ ...item, sourceType: 'book' })),
                    ...searchData.map(item => ({ ...item, sourceType: 'search' }))
                ].filter(item => {
                    // 在所有字段中搜索
                    return Object.entries(item).some(([key, value]) => {
                        if (key === 'sourceType' || !value) return false;
                        const strValue = String(value).toLowerCase();
                        return strValue.includes(searchTerm);
                    });
                });
            }
        } else {
            // 如果没有搜索词，但有分类，则显示该分类的所有项目
            if (category) {
                const searchData = await readSearchFile();
                results = searchData
                    .filter(item => item.category && item.category.trim() === category)
                    .map(item => ({ ...item, sourceType: 'search' }));
            }
        }

        // 如果有分类参数，进一步过滤结果
        if (category && results.length > 0) {
            results = results.filter(item =>
                item.category && item.category.trim() === category
            );
        }

        // 计算分页
        const totalItems = results.length;
        const totalPages = Math.ceil(totalItems / ITEMS_PER_PAGE);
        const startIndex = (page - 1) * ITEMS_PER_PAGE;
        const paginatedResults = results.slice(startIndex, startIndex + ITEMS_PER_PAGE);
        // 创建一个函数，根据数据类型返回不同的详情页URL
        const getDetailUrl = (item) => {
            if (item.sourceType === 'book') {
                return `/books/detail/${item.id}`;
            } else if (item.sourceType === 'journal') {
                return `/journals/${item.id}`;
            } else if (item.sourceType === 'search') {
                return `/search-detail/${item.id}`;
            } else {
                return `/search`;
            }
        };

        // 获取分类数据用于侧边栏
        const categories = await getSearchCategories();

        res.render('search', {
            title: 'Search',
            currentPage: 'search',
            searchQuery: query || '',
            searchType: searchType || 'all',
            category: category || '',
            categories,
            results: paginatedResults,
            totalItems: totalItems,
            itemsPerPage: ITEMS_PER_PAGE,
            pagination: {
                current: page,
                total: totalPages,
                hasNext: page < totalPages,
                hasPrev: page > 1
            },
            getDetailUrl, // 传递URL生成函数
            error
        });
    } catch (error) {
        res.render('search', {
            title: 'Search',
            currentPage: 'search',
            searchQuery: req.query.query || '',
            searchType: req.query.searchType || 'all',
            category: req.query.category || '',
            categories: [],
            results: [],
            totalItems: 0,
            itemsPerPage: ITEMS_PER_PAGE,
            pagination: {
                current: 1,
                total: 1,
                hasNext: false,
                hasPrev: false
            },
            getDetailUrl: () => '/search', // 默认返回搜索页面
            error: error.message
        });
    }
});

// 添加search-detail路由
app.get('/search-detail/:id', async (req, res) => {
    try {
        const { id } = req.params;
        const searchData = await readSearchFile();
        const categories = await getSearchCategories();

        // 查找匹配的记录
        const detail = searchData.find(item =>
            item.id === id ||
            item.ISBN === id ||
            String(item.id).toLowerCase() === String(id).toLowerCase() ||
            String(item.ISBN).toLowerCase() === String(id).toLowerCase()
        );

        if (!detail) {
            throw new Error('Record not found');
        }

        res.render('search-detail', {
            title: detail.Title || 'Detail',
            currentPage: 'search',
            detail,
            categories,
            error: null,
            searchQuery: ''
        });
    } catch (error) {
        console.error('Error in search-detail route:', error);
        res.render('search-detail', {
            title: 'Detail',
            currentPage: 'search',
            detail: null,
            categories: [],
            error: error.message,
            searchQuery: ''
        });
    }
});

app.get('/journals/:id', async (req, res) => {
    try {
        const journalId = req.params.id;

        // 获取缓存中的出版商数据
        // const publishers = getCache(JOURNAL_PUBLISHERS_CACHE_KEY);

        // 从缓存中获取期刊数据
        const journalsData = await readExcelFile();
        if (!journalsData) {
            // logger.error('No journal data in cache');
            return res.render('detail', {
                detail: null,
                error: 'Journal data not available. Please try again later.',
                publishers: []
            });
        }

        // 查找指定ID的期刊
        const journal = journalsData.find(item =>
            item.id === journalId ||
            item.id?.toString() === journalId
        );

        if (!journal) {
            // logger.error(`Journal not found with ID: ${journalId}`);
            return res.render('detail', {
                detail: null,
                error: 'Book not found',
                // publishers: publishers || []
            });
        }

        // 渲染期刊详情页
        res.render('detail', {
            detail: journal,
            error: null,
            // publishers: publishers || []
        });

    } catch (error) {
        console.error(`Error rendering journal detail page: ${error.message}`, { error });
        // logger.error(`Error rendering journal detail page: ${error.message}`, { error });
        res.render('detail', {
            detail: null,
            error: 'Error loading journal details',
            publishers: []
        });
    }
});

// API routes for potential AJAX calls
app.get('/api/data', async (req, res) => {
    try {
        const data = await readExcelFile();
        res.json(data);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

app.get('/api/search', async (req, res) => {
    try {
        const { query } = req.query;
        if (!query) {
            return res.status(400).json({ error: 'Search query is required' });
        }
        const results = await searchExcelData(query);
        res.json(results);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// Upload page route
app.get('/upload', (req, res) => {
    res.render('upload', {
        title: 'Upload',
        currentPage: 'upload',
        error: null,
        success: null,
        searchQuery: ''
    });
});

// File upload route
app.post('/upload', upload.single('file'), async (req, res) => {
    try {
        if (!req.file) {
            throw new Error('Please select a file to upload');
        }

        // 获取数据类型
        const dataType = req.body.dataType || 'general';

        // 清除相应的缓存
        clearCacheByFileType(dataType);

        // 根据数据类型读取新数据并设置成功消息
        let data = [];
        let successMessage = '';

        // 读取新的数据
        if (dataType === 'books') {
            data = await readBooksFile();
            successMessage = 'Book data file uploaded successfully! Cache updated';
        } else if (dataType === 'search') {
            data = await readSearchFile();
            successMessage = 'Search data file uploaded successfully! Cache updated';
        } else {
            data = await readExcelFile();
            successMessage = 'Journal data file uploaded successfully! Cache updated';
        }

        // 验证上传的文件
        if (!data || data.length === 0) {
            throw new Error('Invalid Excel file format or empty file');
        }

        res.render('upload', {
            title: 'Upload',
            currentPage: 'upload',
            error: null,
            success: successMessage,
            searchQuery: '',
            defaultDataType: dataType
        });
    } catch (error) {
        res.render('upload', {
            title: 'Upload',
            currentPage: 'upload',
            error: error.message,
            success: null,
            searchQuery: '',
            defaultDataType: req.body.dataType
        });
    }
});

// 图书列表路由
app.get('/books', async (req, res) => {
    try {
        const page = parseInt(req.query.page) || 1;
        const category = req.query.category;
        const publisher = req.query.publisher;
        const search = req.query.search || '';
        let books = await readBooksFile();
        const categories = await getBookCategories();
        const publisherCounts = await getBooksPublisherCounts();

        // 根据分类过滤数据
        if (category) {
            books = books.filter(book => {
                const bookCategory = book.category?.toLowerCase() || '';
                return bookCategory === category.toLowerCase();
            });
        }

        // 根据出版商过滤数据
        if (publisher) {
            books = books.filter(book => {
                return book.Publisher && book.Publisher.trim() === publisher;
            });
        }

        // 根据搜索词过滤数据
        if (search) {
            books = books.filter(book => {
                const title = book.Title?.toLowerCase() || '';
                const author = book.Authors?.toLowerCase() || '';
                const publisher = book.Publisher?.toLowerCase() || '';
                const isbn = book.ISBN?.toLowerCase() || '';
                const searchLower = search.toLowerCase();

                return title.includes(searchLower) ||
                    author.includes(searchLower) ||
                    publisher.includes(searchLower) ||
                    isbn.includes(searchLower);
            });
        }

        // 计算分页
        const totalItems = books.length;
        const totalPages = Math.ceil(totalItems / ITEMS_PER_PAGE);
        const startIndex = (page - 1) * ITEMS_PER_PAGE;
        const paginatedBooks = books.slice(startIndex, startIndex + ITEMS_PER_PAGE);

        res.render('books', {
            title: 'Book List',
            currentPage: 'books',
            books: paginatedBooks,
            totalItems: totalItems,
            itemsPerPage: ITEMS_PER_PAGE,
            category: category,
            publisher: publisher,
            search: search,
            categories: categories,
            publisherCounts: publisherCounts,
            pagination: {
                current: page,
                total: totalPages,
                hasNext: page < totalPages,
                hasPrev: page > 1
            },
            error: null,
            searchQuery: ''
        });
    } catch (error) {
        console.error('Error rendering books page:', error);
        res.render('books', {
            title: 'Book List',
            currentPage: 'books',
            books: [],
            totalItems: 0,
            itemsPerPage: ITEMS_PER_PAGE,
            category: req.query.category,
            publisher: req.query.publisher,
            search: req.query.search || '',
            categories: [],
            publisherCounts: [],
            pagination: {
                current: 1,
                total: 1,
                hasNext: false,
                hasPrev: false
            },
            error: error.message,
            searchQuery: ''
        });
    }
});

// 图书搜索路由
app.get('/books/search', async (req, res) => {
    try {
        const { query, searchType } = req.query;
        const page = parseInt(req.query.page) || 1;
        let results = [];
        let error = null;

        if (query) {
            // 获取数据
            const allBooks = await readBooksFile();
            const searchTerm = query.toLowerCase();

            // 根据搜索类型进行筛选
            if (searchType && searchType !== 'all') {
                // 根据searchType筛选对应字段
                switch (searchType) {
                    case 'title':
                        results = allBooks.filter(item =>
                            item.Title && item.Title.toLowerCase().includes(searchTerm)
                        );
                        break;
                    case 'author':
                        results = allBooks.filter(item =>
                            item.Authors && item.Authors.toLowerCase().includes(searchTerm)
                        );
                        break;
                    case 'isbn':
                        results = allBooks.filter(item =>
                            item.ISBN && item.ISBN.toLowerCase().includes(searchTerm)
                        );
                        break;
                    case 'publisher':
                        results = allBooks.filter(item =>
                            item.Publisher && item.Publisher.toLowerCase().includes(searchTerm)
                        );
                        break;
                    default:
                        results = await searchBooks(query);
                }
            } else {
                // 默认全局搜索
                results = await searchBooks(query);
            }
        }

        // 计算分页
        const totalItems = results.length;
        const totalPages = Math.ceil(totalItems / ITEMS_PER_PAGE);
        const startIndex = (page - 1) * ITEMS_PER_PAGE;
        const paginatedResults = results.slice(startIndex, startIndex + ITEMS_PER_PAGE);

        res.render('books-search', {
            title: 'Book Search',
            currentPage: 'books',
            searchQuery: query || '',
            searchType: searchType || 'all',
            results: paginatedResults,
            totalItems: totalItems,
            itemsPerPage: ITEMS_PER_PAGE,
            pagination: {
                current: page,
                total: totalPages,
                hasNext: page < totalPages,
                hasPrev: page > 1
            },
            error
        });
    } catch (error) {
        res.render('books-search', {
            title: 'Book Search',
            currentPage: 'books',
            searchQuery: req.query.query || '',
            searchType: req.query.searchType || 'all',
            results: [],
            totalItems: 0,
            itemsPerPage: ITEMS_PER_PAGE,
            pagination: {
                current: 1,
                total: 1,
                hasNext: false,
                hasPrev: false
            },
            error: error.message
        });
    }
});

// 图书详情路由
app.get('/books/detail/:id', async (req, res) => {
    try {
        const { id } = req.params;
        const books = await readBooksFile();
        const publisherCounts = await getBooksPublisherCounts();

        // 尝试多种方式查找图书
        const book = books.find(item =>
            item.id === id ||
            item.ISBN === id ||
            item.bookId === id ||
            String(item.id).toLowerCase() === String(id).toLowerCase() ||
            String(item.ISBN).toLowerCase() === String(id).toLowerCase() ||
            String(item.bookId).toLowerCase() === String(id).toLowerCase()
        );

        if (!book) {
            throw new Error('Book not found');
        }

        res.render('book-detail', {
            title: book.title || book.name || 'Book Detail',
            currentPage: 'books',
            book,
            publisherCounts,
            error: null,
            searchQuery: ''
        });
    } catch (error) {
        console.error('Error in book detail route:', error);
        res.render('book-detail', {
            title: 'Book Detail',
            currentPage: 'books',
            book: null,
            publisherCounts: [],
            error: error.message,
            searchQuery: ''
        });
    }
});


// 图书上传页面路由
app.get('/books/upload', (req, res) => {
    res.render('upload', {
        title: 'Upload Book Data',
        currentPage: 'books',
        error: null,
        success: null,
        searchQuery: '',
        defaultDataType: 'books'  // 默认选择图书数据类型
    });
});

// 期刊列表路由
app.get('/journals', async (req, res) => {
    try {
        const page = parseInt(req.query.page) || 1;
        const category = req.query.category;
        const publisher = req.query.publisher;
        const search = req.query.search || '';
        let journals = await readExcelFile();
        const categories = await getCategories();
        const publisherCounts = await getPublisherCounts();

        // 根据分类过滤数据
        if (category) {
            journals = journals.filter(journal => {
                const journalCategory = journal.category?.toLowerCase() || '';
                return journalCategory === category.toLowerCase();
            });
        }

        // 根据出版商过滤数据
        if (publisher) {
            journals = journals.filter(journal => {
                return journal.Publisher && journal.Publisher.trim() === publisher;
            });
        }

        // 根据搜索词过滤数据
        if (search) {
            journals = journals.filter(journal => {
                const title = journal.Title?.toLowerCase() || '';
                const author = journal.Authors?.toLowerCase() || '';
                const publisher = journal.Publisher?.toLowerCase() || '';
                const isbn = journal.ISBN?.toLowerCase() || '';
                const searchLower = search.toLowerCase();

                return title.includes(searchLower) ||
                    author.includes(searchLower) ||
                    publisher.includes(searchLower) ||
                    isbn.includes(searchLower);
            });
        }

        // 计算分页
        const totalItems = journals.length;
        const totalPages = Math.ceil(totalItems / ITEMS_PER_PAGE);
        const startIndex = (page - 1) * ITEMS_PER_PAGE;
        const paginatedJournals = journals.slice(startIndex, startIndex + ITEMS_PER_PAGE);

        res.render('journals', {
            title: 'Journal List',
            currentPage: 'journals',
            journals: paginatedJournals,
            totalItems: totalItems,
            itemsPerPage: ITEMS_PER_PAGE,
            category: category,
            publisher: publisher,
            search: search,
            categories: categories,
            publisherCounts: publisherCounts,
            pagination: {
                current: page,
                total: totalPages,
                hasNext: page < totalPages,
                hasPrev: page > 1
            },
            error: null,
            searchQuery: ''
        });
    } catch (error) {
        console.error('Error rendering journals page:', error);
        res.render('journals', {
            title: 'Journal List',
            currentPage: 'journals',
            journals: [],
            totalItems: 0,
            itemsPerPage: ITEMS_PER_PAGE,
            category: req.query.category,
            publisher: req.query.publisher,
            search: req.query.search || '',
            categories: [],
            publisherCounts: [],
            pagination: {
                current: 1,
                total: 1,
                hasNext: false,
                hasPrev: false
            },
            error: error.message,
            searchQuery: ''
        });
    }
});

// 期刊搜索路由
app.get('/journals/search', async (req, res) => {
    try {
        const searchQuery = req.query.q || '';
        const searchType = req.query.type || 'all';
        const page = parseInt(req.query.page || '1', 10);
        const limit = 10; // 每页显示数量

        // 获取缓存中的出版商数据
        const publishers = getCache(JOURNAL_PUBLISHERS_CACHE_KEY);

        // 从缓存中获取期刊数据
        const journalsData = getCache(JOURNALS_CACHE_KEY);
        if (!journalsData) {
            // logger.error('No journal data in cache for search');
            return res.render('journals', {
                journals: [],
                error: 'Journal data not available. Please try again later.',
                publishers: publishers || [],
                pagination: createPaginationObject(0, page, limit),
                searchQuery,
                searchType
            });
        }

        // 过滤数据
        let filteredData = [];
        if (searchQuery) {
            const query = searchQuery.toLowerCase();
            switch (searchType) {
                case 'title':
                    filteredData = journalsData.filter(item =>
                        item.Title && item.Title.toLowerCase().includes(query));
                    break;
                case 'author':
                    filteredData = journalsData.filter(item =>
                        item.Authors && item.Authors.toLowerCase().includes(query));
                    break;
                case 'issn':
                    filteredData = journalsData.filter(item =>
                        item.ISSN && item.ISSN.toLowerCase().includes(query));
                    break;
                case 'publisher':
                    filteredData = journalsData.filter(item =>
                        item.Publisher && item.Publisher.toLowerCase().includes(query));
                    break;
                default: // 'all'
                    filteredData = journalsData.filter(item =>
                        (item.Title && item.Title.toLowerCase().includes(query)) ||
                        (item.Authors && item.Authors.toLowerCase().includes(query)) ||
                        (item.ISSN && item.ISSN.toLowerCase().includes(query)) ||
                        (item.Publisher && item.Publisher.toLowerCase().includes(query)));
            }
        } else {
            filteredData = [...journalsData];
        }

        // 计算分页
        const total = filteredData.length;
        const totalPages = Math.ceil(total / limit);
        const startIndex = (page - 1) * limit;
        const endIndex = startIndex + limit;
        const paginatedData = filteredData.slice(startIndex, endIndex);

        res.render('journals', {
            journals: paginatedData,
            error: null,
            publishers: publishers || [],
            pagination: createPaginationObject(total, page, limit),
            searchQuery,
            searchType
        });

    } catch (error) {
        // logger.error(`Error rendering journal search page: ${error.message}`, { error });
        res.render('journals', {
            journals: [],
            error: 'Error searching journals',
            publishers: [],
            pagination: createPaginationObject(0, 1, 10),
            searchQuery: req.query.q || '',
            searchType: req.query.type || 'all'
        });
    }
});

// 期刊上传页面路由
app.get('/journals/upload', (req, res) => {
    res.render('upload', {
        title: 'Upload Journal Data',
        currentPage: 'journals',
        error: null,
        success: null,
        searchQuery: '',
        defaultDataType: 'general'  // 期刊使用通用数据类型
    });
});

// 搜索数据上传页面路由
app.get('/search/upload', (req, res) => {
    res.render('upload', {
        title: 'Upload Search Data',
        currentPage: 'search',
        error: null,
        success: null,
        searchQuery: '',
        defaultDataType: 'search'  // 默认选择搜索数据类型
    });
});

// Contact page route
app.get('/contact', (req, res) => {
    res.render('contact-result', {
        title: 'Contact Us',
        currentPage: 'contact',
        success: null,
        message: null,
        searchQuery: ''
    });
});

// 图片上传页面路由
app.get('/upload-images', (req, res) => {
    // 从session中获取上传状态和结果
    const imageUrls = req.session.uploadedImages || [];
    const success = req.session.uploadSuccess || null;
    const error = req.session.uploadError || null;

    // 清除session中的数据，避免刷新时重复显示
    req.session.uploadedImages = null;
    req.session.uploadSuccess = null;
    req.session.uploadError = null;

    res.render('upload-images', {
        title: 'Upload Images',
        currentPage: 'upload-images',
        error: error,
        success: success,
        searchQuery: '',
        imageUrls: imageUrls
    });
});

// 图片上传处理路由
app.post('/upload-images', imageUpload.array('images', 10), async (req, res) => {
    try {
        if (!req.files || req.files.length === 0) {
            req.session.uploadError = 'Please select a file to upload';
            return res.redirect('/upload-images');
        }

        // 生成图片URL列表
        const imageUrls = req.files.map(file => {
            // 将文件路径转换为Web可访问的URL
            return `/images/${file.filename}`;
        });

        // 图片上传可能涉及到所有数据的封面图更新，清除所有缓存
        clearAllCache();
        console.log('[Upload] Cleared all caches after image upload');

        // 将上传的图片URLs存储在session中，以便在重定向后显示
        req.session.uploadedImages = imageUrls;
        req.session.uploadSuccess = `Successfully uploaded ${req.files.length} images! All data cache updated.`;

        // 重定向到上传页面，避免表单重新提交
        res.redirect('/upload-images');
    } catch (error) {
        // 将错误消息存储在session中
        req.session.uploadError = error.message;
        res.redirect('/upload-images');
    }
});

// 添加文件修改监听，用于自动清除缓存
const setupFileWatcher = () => {
    try {
        const chokidar = require('chokidar');

        // 监听data目录下的Excel文件变化
        const watcher = chokidar.watch(path.join(__dirname, 'data', '*.xlsx'), {
            persistent: true,
            ignoreInitial: true
        });

        watcher.on('change', (filePath) => {
            console.log(`[FileWatcher] Detected change in file: ${filePath}`);
            const fileName = path.basename(filePath);

            // 根据文件名清除对应的缓存
            if (fileName === 'journals.xlsx') {
                clearCache('journals');
                console.log('[FileWatcher] Cleared journals cache');
            } else if (fileName === 'books.xlsx') {
                clearCache('books');
                console.log('[FileWatcher] Cleared books cache');
            } else if (fileName === 'search.xlsx') {
                clearCache('search');
                console.log('[FileWatcher] Cleared search cache');
            }
        });

        console.log('[FileWatcher] File watcher setup completed');
    } catch (error) {
        console.error('[FileWatcher] Failed to setup file watcher:', error);
        console.log('[FileWatcher] Tip: Run "npm install chokidar" to enable file watching');
    }
};

// 在开发环境下设置文件监听
if (process.env.NODE_ENV !== 'production') {
    setupFileWatcher();
}

// 预热缓存，避免首次访问慢
const prewarmCache = async () => {
    console.log('[Cache] Prewarming cache...');

    try {
        // 预加载期刊数据
        await readExcelFile();
        await getCategories();
        await getPublisherCounts();

        // 预加载图书数据
        await readBooksFile();
        await getBookCategories();
        await getBooksPublisherCounts();

        // 预加载搜索数据
        await readSearchFile();

        console.log('[Cache] Cache prewarming completed');
    } catch (error) {
        console.error('[Cache] Error during cache prewarming:', error);
    }
};

// 在服务器启动时预热缓存
prewarmCache();

// 添加缓存管理API路由
if (process.env.NODE_ENV !== 'production') {
    app.get('/api/cache/status', (req, res) => {
        res.json(getCacheStatus());
    });

    app.post('/api/cache/clear/:type', (req, res) => {
        const { type } = req.params;

        if (type === 'all') {
            clearAllCache();
            res.json({ success: true, message: 'All caches cleared' });
        } else if (['journals', 'books', 'search'].includes(type)) {
            clearCache(type);
            res.json({ success: true, message: `${type} cache cleared` });
        } else {
            res.status(400).json({ success: false, message: 'Invalid cache type' });
        }
    });

    // 添加强制重新加载缓存的API
    app.post('/api/cache/reload/:type', async (req, res) => {
        const { type } = req.params;

        try {
            if (type === 'all') {
                clearAllCache();
                await prewarmCache();
                res.json({ success: true, message: 'All caches reloaded' });
            } else if (type === 'journals') {
                clearCache('journals');
                await readExcelFile();
                await getCategories();
                await getPublisherCounts();
                res.json({ success: true, message: 'Journals cache reloaded' });
            } else if (type === 'books') {
                clearCache('books');
                await readBooksFile();
                await getBookCategories();
                await getBooksPublisherCounts();
                res.json({ success: true, message: 'Books cache reloaded' });
            } else if (type === 'search') {
                clearCache('search');
                await readSearchFile();
                res.json({ success: true, message: 'Search cache reloaded' });
            } else {
                res.status(400).json({ success: false, message: 'Invalid cache type' });
            }
        } catch (error) {
            res.status(500).json({ success: false, message: error.message });
        }
    });
}

// 添加文件监视信息页面（仅开发环境）
if (process.env.NODE_ENV !== 'production') {
    app.get('/dev/cache-info', (req, res) => {
        res.render('dev-cache-info', {
            title: 'Cache Management',
            currentPage: 'dev',
            cacheStatus: getCacheStatus(),
            searchQuery: ''
        });
    });
}

// Start server
app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});
